package com.hspedu.newData.dmsxl.树.二叉搜索树的操作;

import com.hspedu.newData.dmsxl.ds.TreeNode;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-19 
 * @Version: 1.0
 */

public class LC108将有序数组转换成二叉搜索树 {

    public TreeNode sortedArrayToBST(int[] nums) {

        TreeNode treeNode = findNode(nums, 0, nums.length);
        return treeNode;
    }

    public TreeNode findNode(int[] nums,int startIndex,int endIndex){

        if (startIndex >= endIndex){
            return null;
        }

        int midIndex = startIndex + (endIndex - startIndex)/2;

        TreeNode treeNode = new TreeNode(nums[midIndex]);

        treeNode.left = findNode(nums,startIndex,midIndex);
        treeNode.right = findNode(nums,midIndex + 1,endIndex);

        return treeNode;


    }
}
